package org.example.demo4.controller.freight;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Set;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.example.demo4.annotation.Log;
import org.example.demo4.model.SysMenuEntity;
import org.example.demo4.model.SysUserEntity;
import org.example.demo4.service.SysLoginService;
import org.example.demo4.service.SysMenuService;
import org.example.demo4.service.impl.SysPermissionService;
import org.example.demo4.utils.Constants;
import org.example.demo4.utils.SecurityUtils;
import org.example.demo4.utils.enums.BusinessType;
import org.example.demo4.utils.http.AjaxResult;
import org.example.demo4.utils.login.LoginBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Tag(name = "登陆验证管理")
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @Operation(summary="登陆获取token，放在其他请求的head中")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    
    

    /**
     * 登录方法
     *
     * @return 结果
     * @throws UnsupportedEncodingException 
     */
    @PostMapping("/extLogin")
    @Operation(summary="单点登陆")
    @Log(title = "单点登陆", businessType = BusinessType.EXPORT)
    public AjaxResult extLogin(@RequestBody LoginBody loginBody) throws UnsupportedEncodingException {
        String accessToken = loginBody.getExtToken();
        if (StringUtils.isNotEmpty(accessToken)) {
        	accessToken = URLDecoder.decode(accessToken,"utf-8");
            String tokenNew = loginService.extLogin(accessToken);
            AjaxResult ajax = AjaxResult.success();
            ajax.put(Constants.TOKEN, tokenNew);
            return ajax;
           
        } else {
            return AjaxResult.error();
        }
        

        
    }

	
	
	

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    @Operation(summary="获取用户信息")
    public AjaxResult getInfo()
    {
        SysUserEntity user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
//    @Operation(summary="获取路由菜单")
//    @GetMapping("getRouters")
//    public AjaxResult getRouters()
//    {
//        Long userId = SecurityUtils.getUserId();
//        List<SysMenuEntity> menus = menuService.selectMenuTreeByUserId(userId);
//        return AjaxResult.success(menuService.buildMenus(menus));
//    }
}
